package com.mes.web.controller.jy;

import java.text.DecimalFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mes.entity.hy.Work1;
import com.mes.entity.jy.Plandetail;
import com.mes.entity.jy.Queryjy;
import com.mes.service.hy.IWorkService;
import com.mes.service.jy.ProductPlanDetailServise;
import com.mes.service.jy.ProductPlanServise;
import com.mes.service.jy.productChartServise;
import com.mes.utils.ResponseUtil;

/**
 * @ClassName: ProductChartController 
 * @Description:生产图表
 * @Author jy
 * @Date 2018年1月19日 下午2:18:35 
 * @version 1.0
 */
@Controller
@RequestMapping("/jy")
public class ProductChartController {
	

	@Resource
	private ProductPlanServise productPlanServise;
	@Resource
	private ProductPlanDetailServise productPlanDetailServise;
	@Resource
	private productChartServise productChartServise;
	@Resource
	private IWorkService iWorkService;

	/**
	 * 生产数量统计
	 * @param queryjy 条件查询实体
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectProductNum")
	public void selectProductNum(Queryjy queryjy,HttpServletResponse response) throws Exception {
		//实际量
		Integer num  = 0;
		//计划量
		Double plnum = 0.00;
		//完成率
		Double snum = 0.00;
		
		int count = productPlanDetailServise.countProductAllPlanDetailByState(queryjy);
		//车种，品编，品名，颜色，生产数量
		List<Plandetail> list = productPlanDetailServise.selectAllPlanDetail(queryjy);
	
	for (Plandetail plandetail : list) {
		//当前完成数量、剩余数量(根据计划详情id查询)
		List<String> snum2s = productPlanServise.selectWorkBypid(plandetail.getId());
		for (String snum2 : snum2s) {
			if(snum2 != null && snum2 != "") {
				//实际量
	    		num = num + Integer.valueOf(snum2);
	    	}
		}//第二层for
		
		//根据（remark1）
		List<String> snum3s = productPlanServise.selectWorkBypid2(plandetail.getId());
		for (String snum3 : snum3s) {
			if(snum3 != null && snum3 != "") {
				//实际量
    			 num = num + Integer.valueOf(snum3);
    		}
		}//第二层for
		
		//实际量
		plandetail.setRemartk5(num+"");
		//计划量
		plnum = Double.valueOf(plandetail.getRemartk1());
		if(plnum != null) {
			if(num != null) {
				snum = (num/plnum)*100;
	    		DecimalFormat df = new DecimalFormat("#.##");
	    		//完成率
	    		plandetail.setRemartk6(Double.parseDouble(df.format(snum))+"%");
			}
		}
		
		snum2s = null;
		snum3s = null;
		num  = 0;
		plnum = 0.00;
		snum = 0.00;
	}
	
	JSONObject jsonObject=new JSONObject();
	jsonObject.put("total", count);
	jsonObject.put("rows", list);
	ResponseUtil.write(response, jsonObject);
	} 
	
	/**
	 * 品质现况统计
	 * @param response 响应
	 * @param queryjy 查询实体
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectPinzhi")
	public void selectPinzhi(HttpServletResponse response,Queryjy queryjy) throws Exception {
		//车种，品编，品名，颜色，生产数量,不良品数量，不良率，
		List<Work1> list = productChartServise.selectAllWorkPagine(queryjy);
		int count = productChartServise.countAllWork(queryjy);
		Work1 work2=new Work1();
		for (Work1 work1 : list) {
			//根据品编和设备，时间，班次四个条件查询：未成形，毛边，冷料，光印
			queryjy.setDatetime(work1.getDatetime());
			queryjy.setMachin(work1.getMachin());
			queryjy.setNmum(work1.getNmum());
			queryjy.setRemartk(work1.getRemartk());
			work2=productChartServise.selectNGood(queryjy);
			if(work2!= null) {
				work1.setBrightMark(work2.getBrightMark());
				work1.setColdMetal(work2.getColdMetal());
				work1.setnForming(work2.getnForming());
				work1.setDeckleEdge(work2.getDeckleEdge());
			}
			//不良率 = 不良品/（良品+不良品）
			Double good = Double.valueOf(work1.getGood());
			Double ngood =Double.valueOf(work1.getNgood());
			Double rate = ngood/(good+ngood)*100;
			
			DecimalFormat df = new DecimalFormat("#.##");
			work1.setRate(Double.parseDouble(df.format(rate))+"%");
		}
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", count);
		jsonObject.put("rows", list);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 设备启动率（后期待开发功能）
	 */
	@RequestMapping("/selectEqupmentStart")
	public void selectEqupmentStart() {
		//设备编号，设备名称，吨位数，生产数量，启动时间，非启动时间，启动率
	}
}
